package com.imooc.security.core.social;

import com.imooc.security.core.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter{
    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired(required = false)
    private ConnectionSignUp connectionSignUp;

    @Autowired(required = false)
    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;




    /**
     *配置与数据库连接的操作
     * @param connectionFactoryLocator 查找我们自定义的 ConnectionFactory
     * @return
     */
    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        /** 最后一个参数的作用是对数据库里的数据进行加解密操作 目前不做加解密操作*/
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
        repository.setTablePrefix("imooc_"); //设置标的前缀
        /** 设置自动注册*/
        if (connectionSignUp!=null){
            System.out.println("设置自动注册...."+connectionSignUp);

            repository.setConnectionSignUp(connectionSignUp);

        }
        return repository;
    }

    /**
     * 自定义认证时 的拦截路径
     * 自定义注册路径
     * @return
     */
    @Bean
    public SpringSocialConfigurer imoocSocialConfigurer(){
        String filterProcessesUrl = securityProperties.getSocial().getFilterProcessesUrl();
        System.out.println("filterProcessesUrl::: "+filterProcessesUrl);
        ImoocSpringSocialConfigurer configurer = new ImoocSpringSocialConfigurer(filterProcessesUrl);
        configurer.signupUrl(securityProperties.getBrowser().getSignUpUrl());
        configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);
        return  configurer;
    }

    /**  1. 在注册过程中如何拿到 SpringSocial 的信息; 2.注册完成了 如何把业务系统的用户ID 传给SpringSocial  */

    @Bean
    public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator){
        ProviderSignInUtils providerSignInUtils = new ProviderSignInUtils(connectionFactoryLocator, getUsersConnectionRepository(connectionFactoryLocator));


        return providerSignInUtils;
    }




}

/**
 * 建表语句:
 create table UserConnection (userId varchar(255) not null,        # 我们业务系统的用户id
 providerId varchar(255) not null,                                 # 服务提供商的 ID
 providerUserId varchar(255),                                      # openId    自定义Adapter 中 设置的 ConnectionValues
 rank int not null,                                                # 等级
 displayName varchar(255),                                         # 自定义Adapter 中 设置的 ConnectionValues
 profileUrl varchar(512),                                          # 自定义Adapter 中 设置的 ConnectionValues
 imageUrl varchar(512),                                            # 自定义Adapter 中 设置的 ConnectionValues
 accessToken varchar(512) not null,                                # 当前用户的令牌   与OAuth2协议相关的数据
 secret varchar(512),                                              # 与OAuth2协议相关的数据
 refreshToken varchar(512),                                        # 与OAuth2协议相关的数据
 expireTime bigint,
 primary key (userId, providerId, providerUserId));
 create unique index UserConnectionRank on UserConnection(userId, providerId, rank);
 */
